import os
import sys
import random
import math
import re
import time
import numpy as np
import cv2
import matplotlib
#import matplotlib.pyplot as plt

from config import Config
import utils
import model as modellib
<<<<<<< HEAD
#import visualize
=======
import visualize
>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
from model import log

#%matplotlib inline 

# Root directory of the project
ROOT_DIR = os.getcwd()

# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")

# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
<<<<<<< HEAD

PRETRAINED_MODEL_PATH = './logs/shapes20180315T0318/mask_rcnn_shapes_0006.h5'

=======
>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
    utils.download_trained_weights(COCO_MODEL_PATH)
    
#2    
class ShapesConfig(Config):
    """Configuration for training on the toy shapes dataset.
    Derives from the base Config class and overrides values specific
    to the toy shapes dataset.
    """
    # Give the configuration a recognizable name
    NAME = "shapes"

    # Train on 1 GPU and 8 images per GPU. We can put multiple images on each
    # GPU because the images are small. Batch size is 8 (GPUs * images/GPU).
    GPU_COUNT = 1
<<<<<<< HEAD
    IMAGES_PER_GPU = 64
    
=======
    IMAGES_PER_GPU = 8

>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
    # Number of classes (including background)
    NUM_CLASSES = 1 + 1  # background + 3 shapes

    # Use small images for faster training. Set the limits of the small side
    # the large side, and that determines the image shape.
    IMAGE_MIN_DIM = 128
    IMAGE_MAX_DIM = 128

    # Use smaller anchors because our image and objects are small
    RPN_ANCHOR_SCALES = (8, 16, 32, 64, 128)  # anchor side in pixels

    # Reduce training ROIs per image because the images are small and have
    # few objects. Aim to allow ROI sampling to pick 33% positive ROIs.
    TRAIN_ROIS_PER_IMAGE = 32

    # Use a small epoch since the data is simple
<<<<<<< HEAD
    STEPS_PER_EPOCH = 500

    # use small validation steps since the epoch is small
    VALIDATION_STEPS = 50
=======
    STEPS_PER_EPOCH = 100

    # use small validation steps since the epoch is small
    VALIDATION_STEPS = 5
>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
    
config = ShapesConfig()
config.display()
#3
def get_ax(rows=1, cols=1, size=8):
    """Return a Matplotlib Axes array to be used in
    all visualizations in the notebook. Provide a
    central point to control graph sizes.
    
    Change the default size attribute to control the size
    of rendered images
    """
    _, ax = plt.subplots(rows, cols, figsize=(size*cols, size*rows))
    return ax


#4
class ShapesDataset(utils.Dataset):
    """Generates the shapes synthetic dataset. The dataset consists of simple
    shapes (triangles, squares, circles) placed randomly on a blank surface.
    The images are generated on the fly. No file access required.
    """
<<<<<<< HEAD
    def load_shapes(self, count, height, width,TRAINING_DIR):
=======
    
    def load_shapes(self, count, height, width):
>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
        """Generate the requested number of synthetic images.
        count: number of images to generate.
        height, width: the size of the generated images.
        """
        # Add classes
        self.add_class("shapes", 1, "road")
        
<<<<<<< HEAD
        #lvsolo
        TRAINING_IMAGES_DIR = TRAINING_DIR + 'images/'
        
=======
>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
        # Add images
        # Generate random specifications of images (i.e. color and
        # list of shapes sizes and locations). This is more compact than
        # actual images. Images are generated on the fly in load_image().
<<<<<<< HEAD
        
        #TRAINING_DIR = '/home/lvshoulu/test-env3/src/dataset/luwang/now/'
        #images_paths = os.listdir('/home/lvshoulu/test-env3/src/dataset/luwang/now/images')
        #TRAINING_DIR = '/home/lvshoulu/test-env3/src/dataset/luwang/now/'
        images_names = os.listdir(TRAINING_IMAGES_DIR)
        for i, image_name in enumerate(images_names):
            if i > count-1:return
            #bg_color, shapes = self.random_image(height, width)
            self.add_image("shapes", image_id=i, path=TRAINING_IMAGES_DIR+image_name,
                           bg_color=[0,0,0])
            print('adding '+TRAINING_IMAGES_DIR+image_name+'to training images')
    def load_image(self, image_id):
        """Generate an image from the specs of the given image ID.
        Typically this function loads the image from a file, but
        in this case it generates the image on the fly from the
        specs in image_info.
        """
        #info = self.image_info[image_id]
        #bg_color = np.array(info['bg_color']).reshape([1, 1, 3])
        #image = np.ones([info['height'], info['width'], 3], dtype=np.uint8)
        #image = image * bg_color.astype(np.uint8)
        #for shape, color, dims in info['shapes']:
        #    image = self.draw_shape(image, shape, dims, color)
        image = cv2.imread(self.image_info[image_id]['path'])
        return image
    def load_validation(self, count, height, width, training_images_paths,TRAINING_DIR):
=======
        TRAINING_DIR = '/home/lvshoulu/test-env3/src/dataset/luwang/now/'
        images_paths = os.listdir('/home/lvshoulu/test-env3/src/dataset/luwang/now/images')
        for i, image_path in enumerate(images_paths):
            if i > count-1:return
            #bg_color, shapes = self.random_image(height, width)
            self.add_image("shapes", image_id=i, path=TRAINING_DIR+'images/'+image_path,
                           bg_color=[0,0,0])
            
    def load_validation(self, count, height, width, training_images_paths):
>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
        """Generate the requested number of synthetic images.
        count: number of images to generate.
        height, width: the size of the generated images.
        """
        # Add classes
        self.add_class("shapes", 1, "road")
        
        # Add images
        # Generate random specifications of images (i.e. color and
        # list of shapes sizes and locations). This is more compact than
        # actual images. Images are generated on the fly in load_image().
<<<<<<< HEAD
        #TRAINING_DIR = '/home/lvshoulu/test-env3/src/dataset/luwang/now/'
        images_paths = os.listdir(TRAINING_DIR+'images')
=======
        TRAINING_DIR = '/home/lvshoulu/test-env3/src/dataset/luwang/now/'
        images_paths = os.listdir('/home/lvshoulu/test-env3/src/dataset/luwang/now/images')
>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
        num=0
        for i, image_path in enumerate(images_paths):
            if num > count-1:return
            #bg_color, shapes = self.random_image(height, width)
<<<<<<< HEAD
            if image_path not in training_images_paths:
                self.add_image("shapes", image_id=i, path=TRAINING_DIR+'images/'+image_path,bg_color=[0,0,0])
                num+=1
                print('validation_image:\n',image_path)
    
=======
            if image-path not in training_images_paths:
                self.add_image("shapes", image_id=i, path=TRAINING_DIR+'images/'+image_path,bg_color=[0,0,0])
                num+=1
                print('validation_image:\n',image_path)
                
    def load_image(self, image_id):
        """Generate an image from the specs of the given image ID.
        Typically this function loads the image from a file, but
        in this case it generates the image on the fly from the
        specs in image_info.
        """
        #info = self.image_info[image_id]
        #bg_color = np.array(info['bg_color']).reshape([1, 1, 3])
        #image = np.ones([info['height'], info['width'], 3], dtype=np.uint8)
        #image = image * bg_color.astype(np.uint8)
        #for shape, color, dims in info['shapes']:
        #    image = self.draw_shape(image, shape, dims, color)
        image = cv2.imread(self.image_info[image_id]['path'])
        return image

>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
    def image_reference(self, image_id):
        """Return the shapes data of the image."""
        info = self.image_info[image_id]
        if info["source"] == "shapes":
            return info["shapes"]
        else:
            super(self.__class__).image_reference(self, image_id)

<<<<<<< HEAD
    def load_mask(self, image_id ):
=======
    def load_mask(self, image_id):
>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
        """Generate instance masks for shapes of the given image ID.
        """
        info = self.image_info[image_id]
        path = info['path']
<<<<<<< HEAD
        split_path_strs = path.split('/')
        TRAINING_DIR =''
        for i in range(len(split_path_strs)-2):
            TRAINING_DIR += ('/'+ split_path_strs[i] )
        name = 'mask' + path.split('/')[-1][5:]
        mask = cv2.imread(TRAINING_DIR + '/masks/'+name)
        #print('loading mask:'+TRAINING_DIR + '/masks/'+name)
        
        #mask = cv2.imread('/home/lvshoulu/test-env3/sorc/dataset/luwang/now/masks/'+name)#, 0)
=======
        name = 'mask' + path.split('/')[-1][5:]
        mask = cv2.imread('/home/lvshoulu/test-env3/src/dataset/luwang/now/masks/'+name)#, 0)
>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
        #print('mask path:'+'/home/lvshoulu/test-env3/src/dataset/luwang/now/masks/'+name)
        #print('mask.shape:',mask.shape)
        class_ids = np.array([1])
#         shapes = info['shapes']
#         count = len(shapes)
#         mask = np.zeros([info['height'], info['width'], count], dtype=np.uint8)
#         for i, (shape, _, dims) in enumerate(info['shapes']):
#             mask[:, :, i:i+1] = self.draw_shape(mask[:, :, i:i+1].copy(),
#                                                 shape, dims, 1)
#         # Handle occlusions
#         occlusion = np.logical_not(mask[:, :, -1]).astype(np.uint8)
#         for i in range(count-2, -1, -1):
#             mask[:, :, i] = mask[:, :, i] * occlusion
#             occlusion = np.logical_and(occlusion, np.logical_not(mask[:, :, i]))
#         # Map class names to class IDs.
#         class_ids = np.array([self.class_names.index(s[0]) for s in shapes])
        return mask, class_ids.astype(np.int32)#np.asarray(class_ids,dtype = 'np.int32')#

    def draw_shape(self, image, shape, dims, color):
        """Draws a shape from the given specs."""
        # Get the center x, y and the size s
        x, y, s = dims
        if shape == 'square':
            cv2.rectangle(image, (x-s, y-s), (x+s, y+s), color, -1)
        elif shape == "circle":
            cv2.circle(image, (x, y), s, color, -1)
        elif shape == "triangle":
            points = np.array([[(x, y-s),
                                (x-s/math.sin(math.radians(60)), y+s),
                                (x+s/math.sin(math.radians(60)), y+s),
                                ]], dtype=np.int32)
            cv2.fillPoly(image, points, color)
        return image

    def random_shape(self, height, width):
        """Generates specifications of a random shape that lies within
        the given height and width boundaries.
        Returns a tuple of three valus:
        * The shape name (square, circle, ...)
        * Shape color: a tuple of 3 values, RGB.
        * Shape dimensions: A tuple of values that define the shape size
                            and location. Differs per shape type.
        """
        # Shape
        shape = random.choice(["square", "circle", "triangle"])
        # Color
        color = tuple([random.randint(0, 255) for _ in range(3)])
        # Center x, y
        buffer = 20
        y = random.randint(buffer, height - buffer - 1)
        x = random.randint(buffer, width - buffer - 1)
        # Size
        s = random.randint(buffer, height//4)
        return shape, color, (x, y, s)

    def random_image(self, height, width):
        """Creates random specifications of an image with multiple shapes.
        Returns the background color of the image and a list of shape
        specifications that can be used to draw the image.
        """
        # Pick random background color
        bg_color = np.array([random.randint(0, 255) for _ in range(3)])
        # Generate a few random shapes and record their
        # bounding boxes
        shapes = []
        boxes = []
        N = random.randint(1, 4)
        for _ in range(N):
            shape, color, dims = self.random_shape(height, width)
            shapes.append((shape, color, dims))
            x, y, s = dims
            boxes.append([y-s, x-s, y+s, x+s])
        # Apply non-max suppression wit 0.3 threshold to avoid
        # shapes covering each other
        keep_ixs = utils.non_max_suppression(np.array(boxes), np.arange(N), 0.3)
        shapes = [s for i, s in enumerate(shapes) if i in keep_ixs]
        return bg_color, shapes
#5
# Training dataset
dataset_train = ShapesDataset()
<<<<<<< HEAD
dataset_train.load_shapes(1000, config.IMAGE_SHAPE[0], config.IMAGE_SHAPE[1],'/home/lvshoulu/test-env3/src/dataset/luwang/Samples/train_valid/')
dataset_train.prepare()
print('train images load finished')

training_images_paths = []
for i in range(1000):
    training_images_paths.append(dataset_train.image_info[i]['path'].split('/')[-1])
#print('training_images_paths:\n',training_images_paths)
=======
dataset_train.load_shapes(500, config.IMAGE_SHAPE[0], config.IMAGE_SHAPE[1])
dataset_train.prepare()

training_images_paths = []
for i in range(500):
    training_images_paths.append(dataset_train.img_info[i]['path'].split('/')[-1])
print('training_images_paths:\n',training_images_paths)
>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
                                 


# Validation dataset
dataset_val = ShapesDataset()
<<<<<<< HEAD
dataset_val.load_validation(100, config.IMAGE_SHAPE[0], config.IMAGE_SHAPE[1],training_images_paths,'/home/lvshoulu/test-env3/src/dataset/luwang/Samples/train_valid/')
dataset_val.prepare()


=======
dataset_val.load_validation(100, config.IMAGE_SHAPE[0], config.IMAGE_SHAPE[1],training_images_paths)
dataset_val.prepare()

>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
#6
# Load and display random samples
# image_ids = np.random.choice(dataset_train.image_ids, 4)
# for image_id in image_ids:
#     image = dataset_train.load_image(image_id)
#     mask, class_ids = dataset_train.load_mask(image_id)
#     visualize.display_top_masks(image, mask, class_ids, dataset_train.class_names)
    
#7
<<<<<<< HEAD
#distribute the GPU_RAM automatically
import keras.backend.tensorflow_backend as KTF
import tensorflow
temp_config = tensorflow.ConfigProto()  
temp_config.gpu_options.allow_growth=True   
session = tensorflow.Session(config=temp_config)
KTF.set_session(session)


=======
>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
# Create model in training mode
model = modellib.MaskRCNN(mode="training", config=config,
                          model_dir=MODEL_DIR)

#8
# Which weights to start with?
init_with = "coco"  # imagenet, coco, or last

if init_with == "imagenet":
    model.load_weights(model.get_imagenet_weights(), by_name=True)
elif init_with == "coco":
    # Load weights trained on MS COCO, but skip layers that
    # are different due to the different number of classes
    # See README for instructions to download the COCO weights
    model.load_weights(COCO_MODEL_PATH, by_name=True,
                       exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", 
                                "mrcnn_bbox", "mrcnn_mask"])
elif init_with == "last":
    # Load the last model you trained and continue training
<<<<<<< HEAD
#    model.load_weights(model.find_last()[1], by_name=True)
    model.load_weights(PRETRAINED_MODEL_PATH, by_name=True)
    print("loaded Pretrained model")
=======
    model.load_weights(model.find_last()[1], by_name=True)

>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
#9
# Train the head branches
# Passing layers="heads" freezes all layers except the head
# layers. You can also pass a regular expression to select
# which layers to train by name pattern.
<<<<<<< HEAD
print('training')
=======
>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
model.train(dataset_train, dataset_val, 
            learning_rate=config.LEARNING_RATE, 
            epochs=1, 
            layers='heads')
<<<<<<< HEAD
print('1:training_finished')
=======

>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
#10
# Fine tune all layers
# Passing layers="all" trains all layers. You can also 
# pass a regular expression to select which layers to
<<<<<<< HEAD
# train by name pattern

=======
# train by name pattern.
>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
model.train(dataset_train, dataset_val, 
            learning_rate=config.LEARNING_RATE / 10,
            epochs=2, 
            layers="all")
<<<<<<< HEAD
print('2:training_finished')
model.train(dataset_train, dataset_val, 
            learning_rate=config.LEARNING_RATE / 20,
            epochs=3, 
            layers="all")
print('3:training_finished')
model.train(dataset_train, dataset_val, 
            learning_rate=config.LEARNING_RATE / 40,
            epochs=4, 
            layers="all")
# print('4:training_finished')
# model.train(dataset_train, dataset_val, 
#             learning_rate=config.LEARNING_RATE / 50,
#             epochs=5, 
#             layers="all")
# print('5:training_finished')
# model.train(dataset_train, dataset_val, 
#             learning_rate=config.LEARNING_RATE / 60,
#             epochs=6, 
#             layers="all")
# print('6:training_finished')
=======

>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a

#11
# Save weights
# Typically not needed because callbacks save after every epoch
# Uncomment to save manually
<<<<<<< HEAD
#model_path = os.path.join(MODEL_DIR, "mask_rcnn_road_lvsolo_20180313.h5")
#model.keras_model.save_weights(model_path)
=======
model_path = os.path.join(MODEL_DIR, "mask_rcnn_road_lvsolo_20180313.h5")
model.keras_model.save_weights(model_path)
>>>>>>> dea63b999ac6ded61cdf270b1090f64fecba4b7a
